<?php

namespace Home\Controller;

class BbsController extends CommonController
{
    protected $page_as = 'bbs';

    // 主题列表
    public function lists()
    {
        $desc = '';
        $c_id = I('get.cat1') ?: I('get.cat', '');
        if ($c_id) {
            $desc = M('Category')->where(compact('c_id'))->getField('c_desc');
        }
        $this->assign('desc', $desc);

        $this->display();
    }

    // 主题详情页
    public function view()
    {
        $this->display();
    }

    // 添加主题
    public function add()
    {
        $SMSopen = C('YTATA_SMS_OPEN');
        $uid     = session('ytata.uid');

        if (empty($SMSopen) && empty($uid)) {
            $this->error('非法请求，短信未配置，不允许快捷发布');
        }

        $this->assign('oauth_list', ytata_get_cache('oauth'));

        $this->display();
    }

    // 保存新发布讨论主题
    public function addsave()
    {
        if (IS_POST) {
            // 用户登录
            if (!D('User')->is_login()) {
                $this->check_smscode_login();  // 短信验证码快捷登录
            }

            $uid  = session('ytata.uid');
            if (empty($uid)) {
                $ret['status']  = false;
                $ret['content'] = "请先登录后操作";
                return $ret;
            }

            $ret = D('Bbs')->bbs_add(I('post.'), $uid);
            if ($ret['status'] === false) {
                $this->error($ret['msg']);
            }
            // 跳转至主题页面
            $this->redirect('Bbs/view', array('id' => $ret['bid']));
        } else {
            $this->error('非法请求');
        }
    }

    // 主题点赞
    public function ajax_inc_praise()
    {
        if (IS_AJAX) {
            $uid       = session('ytata.uid');
            $bid       = I('request.bid', '', 'intval');
            if (empty($uid)) {
                $ajax['status']  = 0;
                $ajax['content'] = "请登录后操作！";
                $this->ajaxReturn($ajax);
            }
            if (empty($uid) || empty($bid)) {
                $ajax['status']  = 0;
                $ajax['content'] = "参数错误";
                $this->ajaxReturn($ajax);
            }

            $r = D('Bbs')->praise($uid, $bid);//增加点赞记录
            if ($r['status'] == 2) {
                $ajax['status']  = 0;
                $ajax['content'] = "你已经赞过了！";
                $this->ajaxReturn($ajax);
            } elseif ($r['status'] == 1) {
                $ajax['status']  = 1;
                $ajax['content'] = $r['count_praise'];
                $this->ajaxReturn($ajax);
            }
        }
    }

    // 主题回复
    public function ajax_bbs_reply_save()
    {
        $bid = I('param.bid', '', 'intval');
        $content = I('param.content', '', 'trim');
        $uid = session('ytata.uid');
        if (empty($uid)) {
            $ajax['status'] = 0;
            $ajax['content'] = '请先登录';
            $this->ajaxReturn($ajax);
        }
        if (empty($content)) {
            $ajax['status'] = 0;
            $ajax['content'] = '请输入回复内容';
            $this->ajaxReturn($ajax);
        }
        $bbs = D('Bbs')->get_bbs_one($bid);
        if (empty($bbs)) {
            $ajax['status'] = 0;
            $ajax['content'] = '主题不存在';
            $this->ajaxReturn($ajax);
        }
        $data['content'] = $content;
        $ret = D('BbsReply')->reply_add($data, $bid, $uid);
        if ($ret['status']) {
            $ajax['status']  = 1;
            $ajax['content'] = '保存成功';
            $this->ajaxReturn($ajax);
        } else {
            $ajax['status']  = 0;
            $ajax['content'] = $ret['msg'];
            $this->ajaxReturn($ajax);
        }
    }

    // 主题回复点赞
    public function bbsreply_praise_save()
    {
        $rid = I('param.rid', '', 'intval');
        $uid = session('ytata.uid');

        if (empty($uid)) {
            $ajax['status'] = 0;
            $ajax['content'] = '请先登录';
            $this->ajaxReturn($ajax);
        }

        $ret = D('BbsReply')->praise($uid, $rid);
        if ($ret['status'] == 1) {
            $ajax['status'] = 1;
            $ajax['content'] = $ret['count_praise'];
            $this->ajaxReturn($ajax);
        } else {
            $ajax['status'] = 0;
            $ajax['content'] = $ret['msg'];
            $this->ajaxReturn($ajax);
        }

    }
}